﻿using System;
using System.Web.Mvc;
using CafeOnline.Common;
using System.ComponentModel.Composition;
using CafeOnline.Helpers;

namespace CafeOnline.Controllers {
    [Export(typeof(IController)), ExportMetadata("Name", ControllerNames.UPLOAD_CONTROLLER)]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class UploadController : BaseController {
        [Import]
        private DiskFileStore m_fileStore;
        [HttpPost]
        public JsonResult Upload() {
            string fileName = Request.Headers["X-File-Name"];
            string fileType = Request.Headers["X-File-Type"];
            int fileSize = Convert.ToInt32(Request.Headers["X-File-Size"]);
            //File's content is available in Request.InputStream property
            string ext = fileName.Substring(fileName.LastIndexOf(".")+1);
            var guid = m_fileStore.SaveUploadedFile(Request.InputStream, ext);
            return Json(Url.Content(string.Format("~/Upload/{0}.{1}", guid.ToString(), ext)));
        }
    }
}
